package com.four.user.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
	private static final String url = "jdbc:mysql://192.168.66.128:3306/jxgl  ?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8";
	private static final String user = "zsf";
	private static final String password = "zsf20040304";
	static {
		try {
			DriverManager.registerDriver(new com.mysql.jdbc.Driver());
		}catch (SQLException e) {
			e.printStackTrace();
		}
	}
		public static Connection getConnection() {
			Connection conn = null;
			try {
				conn = DriverManager.getConnection(url, user, password);
			}catch(SQLException e) {
				e.printStackTrace();
			}
			return conn;
		}
		public static void closeResource(Connection conn, PreparedStatement ps, ResultSet rs) {
			try {
				if(rs != null && !rs.isClosed()) {
					rs.close();
				}
			}catch(SQLException e) {
				e.printStackTrace();
			}
			try {
				if(ps!=null && !ps.isClosed()) {
					ps.close();
				}
			}catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(conn != null && !conn.isClosed()) {
					conn.close();
				}
			}catch(SQLException e) {
				e.printStackTrace();
			}
		
//		public static void main(String[] args) {
//			String studentName ="";
//			Connection conn = getConnection();//获取连接
//			String sql = "select * from course";//准备SQL
//			PreparedStatement ps = null;
//			ResultSet rs =null;
//			try {
//获取声明：预载SQL
//				ps = conn.prepareStatement(sql);
//PreparedStatement ps = conn.prepareStatement(sql);
//执行SQL并返回结果
//				rs = ps.executeQuery();
//ResultSet rs = ps.executeQuery();
//				while(rs.next()) {//next():游标下移一位，如果成功则返回true，反之则返回false
//					String no = rs.getString(1);	//获取当前下一行的第一列数据
//					String name = rs.getString("c_name");//通过字段名称获取这个字段的数据
//					System.out.println(no + "," + name);
//				}
//rs.close();
//ps.close();
//conn.close();
//			}catch(SQLException e) {
//				e.printStackTrace();
//			}finally {
//调用关闭资源的方法 
//				closeResource(conn, ps, rs);
			}
		}
//	}
